package com.qlk.vorder.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.qlk.vorder.domain.Task;
import com.qlk.vorder.domain.query.TaskQuery;
import com.qlk.vorder.manager.TaskManager;
import com.qlk.vorder.service.TaskService;
import com.qlk.vorder.utils.exceptions.ExistedException;
import com.qlk.common.utils.page.PageUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.perf4j.aop.Profiled;

/**
 * TaskService接口的实现类
 * 
 * @author zhaoyanan
 * @version 2016-7-28 14:14:00
 * 
 */
@Service
public class TaskServiceImpl implements TaskService {

	/** LOG */
	private static final Log LOG = LogFactory.getLog(TaskServiceImpl.class);

	@Autowired
	private TaskManager taskManager;

	/**
	 * {@inheritDoc}
	 */
	@Profiled(tag = "TaskService.batchInsert")
	public boolean insert(List<Task> taskList) {
		boolean resultFlag = false;
		try {
			if (CollectionUtils.isNotEmpty(taskList)) {
				resultFlag = taskManager.insert(taskList);
			} else {
				LOG.warn("TaskServiceImpl#batchInsert failed, param is illegal.");
			}
		} catch (Exception e) {
			LOG.error("TaskServiceImpl#batchInsert has error.", e);
		}
		return resultFlag;
	}

	/**
	 * {@inheritDoc}
	 */
	@Profiled(tag = "TaskService.insert")
	public boolean insert(Task task) {
		boolean resultFlag = false;
		try {
			if (null != task) {
				if (taskManager.exist(task)) {
					throw new ExistedException();
				}
				resultFlag = taskManager.insert(task);
			} else {
				LOG.warn("TaskServiceImpl#insert failed, param is illegal.");
			}
		} catch (ExistedException e) {
			LOG.warn("TaskServiceImpl#insert failed, task has existed.");
			throw e;
		} catch (Exception e) {
			LOG.error("TaskServiceImpl#insert has error.", e);
		}
		return resultFlag;
	}

	/**
	 * {@inheritDoc}
	 */
	@Profiled(tag = "TaskService.update")
	public boolean update(Task task) {
		boolean resultFlag = false;
		try {
			if (null != task) {
				resultFlag = taskManager.update(task);
			} else {
				LOG.warn("TaskServiceImpl#update failed, param is illegal.");
			}
		} catch (Exception e) {
			LOG.error("TaskServiceImpl#update has error.", e);
		}
		return resultFlag;
	}

	/**
	 * {@inheritDoc}
	 */
	@Profiled(tag = "TaskService.queryTaskList")
	public List<Task> queryTaskList(TaskQuery queryBean) {
		List<Task> taskList = null;
		try {
			taskList = taskManager.queryTaskList(queryBean);
		} catch (Exception e) {
			LOG.error("TaskServiceImpl#queryTaskList has error.", e);
		}
		return taskList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Profiled(tag = "TaskService.queryTaskListWithPage")
	public List<Task> queryTaskListWithPage(TaskQuery queryBean, PageUtil pageUtil) {
		List<Task> taskList = null;
		try {
			taskList = taskManager.queryTaskListWithPage(queryBean, pageUtil);
		} catch (Exception e) {
			LOG.error("TaskServiceImpl#queryTaskListWithPage has error.", e);
		}
		return taskList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Profiled(tag = "TaskService.delete")
	public boolean delete(Task task) {
		boolean resultFlag = false;
		try {
			if (null != task && null != task.getTaskId()) {
				resultFlag = taskManager.delete(task);
			} else {
				LOG.warn("TaskServiceImpl#delete param is illegal.");
			}
		} catch (Exception e) {
			LOG.error("TaskServiceImpl#delete has error.", e);
		}
		return resultFlag;
	}

	/**
	 * {@inheritDoc}
	 */
	@Profiled(tag = "TaskService.batchDelete")
	public boolean delete(Task[] tasks) {
		boolean resultFlag = false;
		try {
			if (null != tasks && tasks.length > 0) {
				resultFlag = taskManager.delete(tasks);
			} else {
				LOG.warn("TaskServiceImpl#batchDelete failed, param is illegal.");
			}
		} catch (Exception e) {
			LOG.error("TaskServiceImpl#batchDelete has error.", e);
		}
		return resultFlag;
	}

	/**
	 * {@inheritDoc}
	 */
	@Profiled(tag = "TaskService.getTaskById")
	public Task getTaskById(Long id) {
		Task task = null;
		try {
			if (null != id) {
				task = taskManager.getTaskById(id);
			} else {
				LOG.warn("TaskServiceImpl#getTaskById failed, param is illegal.");
			}
		} catch (Exception e) {
			LOG.error("TaskServiceImpl#getTaskById has error.", e);
		}
		return task;
	}
}
